package com.showde.shoppingmall.activity;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.kim.androidbase.imageloader.ImageLoader;
import com.showde.shoppingmall.R;
import com.showde.shoppingmall.adapter.HotProductViewPagerAdapter;
import com.showde.shoppingmall.business.logic.CallbackData;
import com.showde.shoppingmall.business.logic.ProductAHRFetchOnCriteriaLogic;
import com.showde.shoppingmall.business.logic.UICallbackInterface;
import com.showde.shoppingmall.db.ProductAHRDBCommand;
import com.showde.shoppingmall.domain.ProductAHR;
import com.showde.shoppingmall.domain.ProductCategory;
import com.showde.shoppingmall.netentity.ProductSearchEntity.FETCH_TYPE;
import com.showde.shoppingmall.utils.ActivityDispatch;

/**
 * 我的街页面
 * 
 * @author
 * 
 */
public class MyStreetActivity extends BaseActivity {

	private static final String TAG = "MyStreetActivity";

	private static final int MSG_FETCH_HOT = 0x10001;
	private static final int MSG_FETCH_ADVERTISEMENT = 0x10002;

	ImageView searchButton; // 搜藏按钮

	ViewPager hotProdutViewPager; // 广告商品

	ImageView productR1ImageView; // 推荐商品1
	TextView productR2ImageView; // 推荐商品2
	TextView productR3ImageView; // 推荐商品3
	ImageView productR4ImageView; // 推荐商品4

	private ImageView tmh; // 特卖会
	private ImageView phb; // 排行榜

	List<ImageView> recommendImageView = new ArrayList<ImageView>();

	ProductAHRDBCommand productDBCommand;

	List<ProductAHR> productHot;
	List<ProductAHR> productAdvertisement;

	ImageLoader imageLoader;

	Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case MSG_FETCH_ADVERTISEMENT:
				refreshAdvertisement();
				break;
			case MSG_FETCH_HOT:
				refreshHotData();
				break;
			}
			super.handleMessage(msg);
		}

	};

	@Override
	protected void initParam() {
		if (productDBCommand == null) {
			productDBCommand = new ProductAHRDBCommand(this);
		}
		fetchAdvertisementFromDB();
		fetchHotFromDB();
		imageLoader = new ImageLoader(this);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	protected void onResume() {
		super.onResume();
		fetchAdvertisement();
		refreshAdvertisement();
		fetchHot();
		refreshHotData();
	}

	@Override
	protected void initView() {
		setContentView(R.layout.my_street_layout);
		searchButton = (ImageView) findViewById(R.id.img_search);
		productR1ImageView = (ImageView) findViewById(R.id.img_r1);
		productR2ImageView = (TextView) findViewById(R.id.img_r2);
		productR3ImageView = (TextView) findViewById(R.id.img_r3);
		productR4ImageView = (ImageView) findViewById(R.id.img_r4);
		hotProdutViewPager = (ViewPager) findViewById(R.id.vp_hotproduct);
		searchButton.setOnClickListener(this);
		productR1ImageView.setOnClickListener(this);
		productR2ImageView.setOnClickListener(this);
		productR3ImageView.setOnClickListener(this);
		productR4ImageView.setOnClickListener(this);

		recommendImageView.add(productR1ImageView);
		// recommendImageView.add(productR2ImageView);
		// recommendImageView.add(productR3ImageView);
		recommendImageView.add(productR4ImageView);

		tmh = (ImageView) findViewById(R.id.img_tmh);
		phb = (ImageView) findViewById(R.id.img_phb);

		tmh.setOnClickListener(this);
		phb.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.img_search:
			// ActivityDispatch.toProductDetails(MyStreetActivity.this);
			ActivityDispatch.openNewActivity(this, SearchResultActivity.class,
					MyStreetGroup.group, "ProductDetailsActivity",
					ActivityDispatch.GROUP_STREET);
			break;
		case R.id.img_r1:
		case R.id.img_r2:
		case R.id.img_r3:
		case R.id.img_r4:
			toHotProductDetails(v.getId());
			break;
		case R.id.img_tmh:
			toProductList("tmh");
			break;
		case R.id.img_phb:
			toProductList("phb");
			break;
		default:
			break;
		}
	}

	private void toProductList(String flag) {

		// flag: tmh
		// flag: phb

		Bundle bundle = new Bundle();
		bundle.putString(ActivityDispatch.PRODUCT_RECOMMEND, flag);
		ActivityDispatch.openNewActivity(this, ProductListActivity.class,
				MyStreetGroup.group, "MyStreetActivity", bundle,
				ActivityDispatch.GROUP_STREET);
	}

	private void toHotProductDetails(int vId) {
		Bundle bundle = new Bundle();
		String productId = null;
		ProductAHR productAHR = null;
		switch (vId) {
		case R.id.img_r1:
			if (productHot == null || productHot.size() == 0) {
				return;
			}
			productAHR = productHot.get(0);
			if (productAHR != null) {
				productId = productAHR.getProductID();
			}
			break;
		case R.id.img_r2:
			// productId = productHot.get(1).getProductID();
			return;
		case R.id.img_r3:
			// productId = productHot.get(2).getProductID();
			return;
		case R.id.img_r4:
			if (productHot == null || productHot.size() == 0) {
				return;
			}
			productAHR = productHot.get(1);
			if (productAHR != null) {
				productId = productAHR.getProductID();
			}
			// productId = productHot.get(3).getProductID();
			break;
		}
		bundle.putString(ActivityDispatch.PRODUCT_ID, productId);
		ActivityDispatch.openNewActivity(this, ProductDetailsActivity.class,
				MyStreetGroup.group, "ProductDetailsActivity", bundle,
				ActivityDispatch.GROUP_STREET);
	}

	/**
	 * 获取广告类商品
	 */
	private void fetchAdvertisement() {
		ProductAHRFetchOnCriteriaLogic.fetch(this,
				FETCH_TYPE.SEARCH_TYPE_ADVERTISEMENT,
				new UICallbackInterface() {

					@Override
					public void onSuccess(CallbackData data) {
						if (data != null && data.data != null)
							handler.sendEmptyMessage(MSG_FETCH_ADVERTISEMENT);
					}

					@Override
					public void onError(String errorMsg) {
						Log.d(TAG, "result::" + errorMsg);
					}
				});
	}

	/**
	 * 获取推荐类商品
	 */
	private void fetchHot() {
		ProductAHRFetchOnCriteriaLogic.fetch(this, FETCH_TYPE.SEARCH_TYPE_HOT,
				new UICallbackInterface() {

					@Override
					public void onSuccess(CallbackData data) {
						if (data != null && data.data != null)
							handler.sendEmptyMessage(MSG_FETCH_HOT);
					}

					@Override
					public void onError(String errorMsg) {
						Log.d(TAG, "result::" + errorMsg);
					}
				});
	}

	private void fetchAdvertisementFromDB() {
		productAdvertisement = productDBCommand.queryProductAdvertisement();
	}

	private void fetchHotFromDB() {
		productHot = productDBCommand.queryProductHot();
	}

	private void refreshAdvertisementData() {
		hotProdutViewPager.setAdapter(new HotProductViewPagerAdapter(this,
				productAdvertisement));
	}

	private void refreshHotData() {
		fetchHotFromDB();
		if (productHot == null || productHot.size() == 0) {
			return;
		}

		for (int i = 0; i < productHot.size(); i++) {
			if (i >= recommendImageView.size()) {
				return;
			}
			imageLoader.displayImage(productHot.get(i).getProductLogo(),
					recommendImageView.get(i));
		}
	}

	private void refreshAdvertisement() {
		fetchAdvertisementFromDB();
		refreshAdvertisementData();
	}

}
